/**
 * 
 */
package org.xlab.baseframework.web.component;

import org.xlab.baseframework.system.Declare;
import org.xlab.baseframework.web.BaseframeworkApplication;
import org.xlab.baseframework.web.util.SpringContextHelper;

import com.vaadin.ui.Layout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;

/**
 * @author MaKuBEX
 * 
 */
public class TopMenuLayout extends AbstractBaseVerticalLayout {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 1692172915982658016L;

	private MenuBar menubar = new MenuBar();

	public TopMenuLayout() {
		menubar.setWidth(Declare.HUNDRED_PERCENT);

		final MenuItem function = menubar.addItem("Function", null);
		final MenuItem logout = menubar.addItem("Logout", new Command() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 1091169075735501518L;

			@Override
			public void menuSelected(MenuItem selectedItem) {
				BaseframeworkApplication app = (BaseframeworkApplication) TopMenuLayout.this
						.getApplication();
				app.setUser(null);
				app.mainWindow.clearMainLayout();
				app.mainWindow.showWelcome();
			}

		});
		logout.setDescription("Logout to the system!");
		final MenuItem codeItem = function.addItem("Code", new Command() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 1091169075735101518L;

			public void menuSelected(MenuItem selectedItem) {
				BaseframeworkApplication app = (BaseframeworkApplication) TopMenuLayout.super
						.getApplication();
				SpringContextHelper helper = new SpringContextHelper(app);
				Layout newLayout = (Layout) helper
						.getBean("bpf_codeFunctionLayout");
				TopMenuLayout.super.getMainWindow().updateMainLayout(newLayout);
			}
		});
		codeItem.setDescription("Code management function");
		final MenuItem paramItem = function.addItem("Parameter", new Command() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 1515301035781577903L;

			public void menuSelected(MenuItem selectedItem) {
				BaseframeworkApplication app = (BaseframeworkApplication) TopMenuLayout.super
						.getApplication();
				SpringContextHelper helper = new SpringContextHelper(app);
				Layout newLayout = (Layout) helper
						.getBean("bpf_parameterFunctionLayout");
				TopMenuLayout.super.getMainWindow().updateMainLayout(newLayout);
			}
		});
		paramItem.setDescription("System parameter management function");
		final MenuItem messageItem = function.addItem("Message", null);
		messageItem.setEnabled(false);
		function.addSeparator();
		final MenuItem logItem = function.addItem("Log", new Command() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -7867328059508961034L;

			@Override
			public void menuSelected(MenuItem selectedItem) {
				BaseframeworkApplication app = (BaseframeworkApplication) TopMenuLayout.super
						.getApplication();
				SpringContextHelper helper = new SpringContextHelper(app);
				Layout newLayout = (Layout) helper
						.getBean("bpf_logFunctionLayout");
				TopMenuLayout.super.getMainWindow().updateMainLayout(newLayout);
			}
		});
		logItem.setDescription("System log management function");
		addComponent(menubar);
	}

}
